package com.cloudcc.mobile.manager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.cloudcc.cloudframe.model.MenuModel;
import com.cloudcc.cloudframe.model.MenuTabModel;
import com.cloudcc.cloudframe.util.ListUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.ExtraConstant;
import com.cloudcc.mobile.bull.GongniuCheckinActivity;
import com.cloudcc.mobile.view.activity.BeauListActivity;
import com.cloudcc.mobile.view.attendance.AttendanceMainActivity;
import com.cloudcc.mobile.view.fragment.BaobiaoDetailActivity;
import com.cloudcc.mobile.view.main.MainUIActivity;
import com.cloudcc.mobile.view.web.NewWebView;
import com.cloudcc.mobile.view.web.Newwebview1;
import com.litesuits.android.log.Log;
import com.litesuits.common.utils.RandomUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangzhiqiang
 * @Description: 菜单管理器
 * @date 2015-10-28 下午4:31:29
 */
public class MenuManager {

    private static MenuManager instance;
    private Context context;

    private MenuManager(Context context) {
        this.context = context;
    }

    public static synchronized MenuManager getInstance() {
        if (instance == null) {
            instance = new MenuManager(AppContext.getInstance());
        }
        return instance;
    }

    /**
     * 过滤菜单
     *
     * @param menus
     */
    public void filterMenus(List<MenuModel> menus) {
        for (MenuModel menuItem : menus) {
            if (ListUtils.isEmpty(menuItem.tab)) {
                continue;
            }
            // menuItem.tab.remove(0);// 删除第一个主页
            List<MenuTabModel> menuChilds = new ArrayList<MenuTabModel>();
            for (int i = 0; i < menuItem.tab.size(); i++) {
                MenuTabModel menuChild = menuItem.tab.get(i);
                if (StringUtils.isBlank(menuChild.url)) {// 子菜单url==null的移除
                    continue;
                }
                if (StringUtils.contains(menuChild.url, "/task.action?")) {// 同事圈
                    continue;
                }
                if (!filtrTab(menuChild)) {// 过滤看板
                    continue;
                }
                fillIcon(menuChild);
                menuChilds.add(menuChild);
            }
            menuItem.tab = menuChilds;
        }
    }

    /**
     * 过滤菜单 是否满足显示条件
     *
     * @return
     */
    private boolean filtrTab(MenuTabModel tab) {
        if (StringUtils.contains(tab.url, "/query.action?obj=")) {// 对象
            return true;
        }
        if ((StringUtils.contains(tab.url, "/controller.action")// 自定义页面
                || StringUtils.contains(tab.url, "/reportList.action")// 报表
                || StringUtils.contains(tab.url, "/viewDashboard.action"))// 看板
                || StringUtils.contains(tab.url,
                "/controller.action?name=tzdzhb")
                || StringUtils.contains(tab.url,
                "salesCloudDashboard.action?m=query")) {
            return true;
        }
        return false;
    }

    /**
     * 设置图标
     *
     * @param tab
     */
    private void fillIcon(MenuTabModel tab) {
        if (tab == null) {
            return;
        }

        // 日报工
        if (StringUtils.containsIgnoreCase(tab.url, "/query.action?obj=rep")) {
            tab.imageId = "cloudtab_baogong";
            return;
        }
        // 报表
        if (StringUtils.containsIgnoreCase(tab.url, "/viewDashboard.action")) {
            tab.imageId = "cloudtabviewboard";
            return;
        }
        // 仪表板（mobile）
        if (StringUtils.containsIgnoreCase(tab.url,
                "/wei_mobiledashboard.action")
                || StringUtils.containsIgnoreCase(tab.url,
                "/controller.action?name=mobiledashboard")) {
            tab.imageId = "cloudtab_board";
            return;
        }
        // 报表 cloudtab_report
        if (StringUtils.containsIgnoreCase(tab.url, "/reportList.action")) {
            tab.imageId = "cloudtab_report";
            return;
        }
        // 为了兼容处理
        if ((StringUtils.containsIgnoreCase(tab.url, "/controller.action"))) {// 自定义页面
            tab.imageId = "cloudtabcustompage";
            return;
        }
        // 尽量适配吧
        if ((StringUtils.equalsIgnoreCase(tab.tab_name, "派工单"))) {
            tab.imageId = "cloutab_dispatch";
            return;
        }
        // 标准库
        if (isMustEntity(tab.url)) {
            tab.imageId = "cloudtab_must_" + getObjID(tab.url);
            return;
        }
        if (StringUtils.isNoneBlank(tab.tab_style)) {
            tab.imageId = tab.tab_style;
            return;
        }
        // random
        int random = RandomUtil.getRandom(10);
        tab.imageId = "cloudtab" + random;
    }

    /**
     * 标准库对象
     */
    private final static String[] MUSTOBJARRAY = new String[]{"001",// 客户
            "002",// 夜壶机会
            "003",// 联系人
            "004",// 潜在客户
            "006",// 市场活动
            "007",// 合同
            "008",// 个案
            "009",// 产品
            "011",// 报价单
            "012",// 报价单明细
            "013",// 解决方案
            "019",// 收款计划
    };

    public String getObjID(String url) {
        String objIdc = url.substring(url.lastIndexOf("=") + 1, url.length());
        return objIdc;
    }

    /**
     * 判断一个 对象是不是标准对象
     *
     * @param url
     * @return true 是标准对象 false ：不是标准对象
     */
    public boolean isMustEntity(String url) {
        if (!isEntityMenuTab(url)) {
            return false;
        }
        String objIdc = getObjID(url);
        for (String objId : MUSTOBJARRAY) {
            if (StringUtils.equals(objIdc, objId)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据连接判断 是不是 对象 的 菜单
     *
     * @param url 对象 /query.action?obj=
     * @return
     */
    public boolean isEntityMenuTab(String url) {
        if (StringUtils.contains(url, "query.action?obj=rep")) {
            return false;
        }
        return StringUtils.contains(url, "/query.action?obj=");
    }

    /**
     * 进入 选项卡页面
     *
     * @param menuTab
     * @param activity
     */
    public void enterMenuDetailOrList(MenuTabModel menuTab, Activity activity) {
        if ("acf100009".equals(menuTab.id)) {
            Intent intent = new Intent(activity, NewWebView.class);
            activity.startActivity(intent);
        } else if (!isEntityMenuTab(menuTab.url)) {
            if (menuTab.tabName.equals("报表")) {//如果是报表走这里
                Intent intent = new Intent(activity, BaobiaoDetailActivity.class);
                intent.putExtra(ExtraConstant.EXTRA_ID, "report");
                intent.putExtra(ExtraConstant.EXTRA_NAME, menuTab.tabName);
                intent.putExtra("imageId", menuTab.imageId);
                intent.putExtra(ExtraConstant.EXTRA_URL, UrlManager.getManager()
                        .getNewRecordUrl(getObjID(menuTab.url)));
                activity.startActivity(intent);
            }else {
                Log.d("clickshanping", "闪屏测试:点击跳转第er步" + menuTab.id + "::" + menuTab.imageId + "::" + menuTab.objId);
                Intent intent = new Intent(activity, NewWebView.class);
                activity.startActivity(intent);
            }
        } else if ("attendance".equals(menuTab.objId)
                || "考勤".equals(menuTab.tabName)) {
            Intent intent = new Intent(activity, AttendanceMainActivity.class);
            activity.startActivity(intent);


        }else if("考勤信息".equals(menuTab.tabName)){
             Intent intent = new Intent(activity,
                    GongniuCheckinActivity.class);
            activity.startActivity(intent);
        } else {
            Intent intent = new Intent(activity, BeauListActivity.class);
            intent.putExtra(ExtraConstant.EXTRA_ID, menuTab.objId);
            intent.putExtra(ExtraConstant.EXTRA_NAME, menuTab.tabName);
            intent.putExtra("imageId", menuTab.imageId);
            intent.putExtra(ExtraConstant.EXTRA_URL, UrlManager.getManager()
                    .getNewRecordUrl(getObjID(menuTab.url)));
            activity.startActivity(intent);
        }
    }

}
